<template>
  <!-- 摄像头列表组件 -->
  <div class="allcameramain">
    <!-- 摄像头列表 -->
    <div class="cameralistmain">
      <div class="camera-title">摄像头列表</div>
      <div v-if="cameraList[0]" class="camera-zlist">
        <div
          v-for="(item, index) in cameraList"
          :key="index"
          class="camera-item"
          :class="cameraId === item.camera.cameraId ? 'beselect' : ''"
          @click="getcamermes(item.camera.cameraId, item.camera.startUp)"
        >
          <i class="el-icon-video-camera"></i>
          {{ item.camera.cameraName }}
        </div>
      </div>
      <!-- 判断有没有摄像头数据 -->
      <div v-if="!cameraList[0]" class="camera-zlist zlistbtn">
        <div class="camera-item btn">暂无摄像头</div>
      </div>
    </div>
    <!-- 摄像头控制按钮 -->
    <!-- <div class="cameracontrol">
      <div class="controne">
        <div class="contro-m z-top" @click="startcontrol(0)">
          <i class="el-icon-caret-top"></i>
        </div>
        <div class="contro-m z-left" @click="startcontrol(2)">
          <i class="el-icon-caret-left"></i>
        </div>
        <div class="contro-m z-right" @click="startcontrol(3)">
          <i class="el-icon-caret-right"></i>
        </div>
        <div class="contro-m z-bottom" @click="startcontrol(1)">
          <i class="el-icon-caret-bottom"></i>
        </div>
        <div class="contro-m z-mirror" @click="mirrorcontrol()">
          <i class="el-icon-refresh-right"></i>
        </div>
      </div>
      <div class="controtwo">
        <div class="contro-left" @click="startcontrol(10)">焦距
          <i class="el-icon-plus"></i>
        </div>
        <div class="contro-right" @click="startcontrol(11)">焦距
          <i class="el-icon-minus"></i></div>
      </div>
      <div class="controthree">
        <div class="greenbut" @click="startcontrol(8)">
          <i class="el-icon-zoom-in"></i> 放大
        </div>
        <div class="greenbut" @click="startcontrol(9)">
          <i class="el-icon-zoom-out"></i>缩小
        </div>
      </div>
    </div>-->
  </div>
</template>
<script>
import nowpagereq from "@/api/digital-production/material-link-monitoring/video-surveillance/index";
import Axios from "axios";
// import throttle from 'lodash/throttle'
import debounce from 'lodash/debounce'
export default {
  props: {
    cameralistparam: {
      type: Object,
      default: () => {
        sessionStorage.organizationNo;
      }
    }
  },
  data() {
    return {
      // 当前摄像头id
      cameraId: "",
      // 当前选中摄像头的详细信息
      cameraInfoList: {},
      // 摄像头列表
      cameraList: [],
      // 网页播放地址
      cameraurl: "",
      startUp: "",
      timer: null,
      timer1: null
    };
  },
  watch: {
    cameralistparam(newval) {
      this.getcameralist();
    }
  },
  mounted() {
    // this.getcameralist()
  },
  beforeDestroy() {
    clearInterval(this.cameraTimer)
    // 离开界面销毁预热
    this.closePreheat();
    clearTimeout(this.timer);
    clearTimeout(this.timer1);
    this.timer = null;
    this.timer1 = null;
    this.cameraId = "";
    this.openSourceMark = "";
  },
  methods: {
    // 获取摄像头列表
    getcameralist() {
      this.$emit("nowcameraurl", "transCamera");
      let params = this.cameralistparam;
      params.organizationNo = sessionStorage.organizationNo;
      this.cameraList = [];
      if (!params.facilityNo) {
        this.timer1 = null;
        clearTimeout(this.timer1);
        return;
      }
      nowpagereq.gethbcameraList(params.facilityNo).then(res => {
        if (res.code === 1) {
          if (res.data.length >= 1) {
            this.cameraList = res.data;
            console.log("列表有摄像头");
            console.log(res.data);
            this.getcamermes(
              res.data[0]["camera"]["cameraId"],
              res.data[0]["camera"]["startUp"]
            );
          } else {
            this.getcamermes(-1, "false");
          }
        } else {
          this.getcamermes(-1, "false");
          this.$emit("nowcameraurl", "notCameraList");
          console.log("列表请求摄像头失败");
        }
      });
    },
    // 获取某个摄像头详细信息
    getcamermes:debounce(async function(id, newStartUp){
      clearTimeout(this.timer1);
      this.timer1 = null;
      this.$emit("nowcameraurl", "transCamera");
      if (this.startUp === "true") {
        this.closePreheat(this.cameraId);
        console.log("销毁预热");
      }
      if (id === -1) {
        this.$emit("nowcameraurl", "notCameraList");
        console.log("列表无摄像头");
        this.cameraId = id;
        this.startUp = newStartUp;
        return;
      }
      this.cameraId = id;
      this.startUp = newStartUp;
      // 注意：startUp是字符串，不是Boolean
      if (newStartUp === "true") {
        this.openSourceMark = new Date().getTime();
        await Axios({
          method: "post",
          url:
            this.$cloudBaseurl + "solarEnergyCamera/openPower",
          data: { cameraId: id, openSourceMark: this.openSourceMark }
        }).then(
          res => {
            console.log(res);
            if (res.data.data.whetherPreheat) {
              // 子向父传值
              this.$emit("getUploadTime", res.data.data.openTime, this.startUp);
              setTimeout(() => {
                this.getSame(id);
              }, res.data.data.openTime * 60000);
            } else {
              this.getSame(id);
            }
          },
          err => {
            console.log(err);
          }
        );
      } else {
        this.getSame(id);
      }
    },1000),
    // 封装获取摄像详细信息
    async getSame(id) {
      if (!(await this.getcams(id)) && id !== -1) {
        this.$emit("nowcameraurl", "downFlow");
        return;
      }
      this.cameraTimer = setInterval(async() => {
        if (!(await this.getcams(id)) && id !== -1) {
          this.$emit("nowcameraurl", "downFlow");
          clearInterval(this.cameraTimer)
        }
      }, 60 * 1000 * 10)
      nowpagereq.getcameramessage(id).then(res => {
        let data = res.data;
        this.cameraInfoList = data.camera;
        this.$emit('cameraInfo', this.cameraInfoList)
        this.setcameraurl();
        console.log("取得摄像头数据进行拼接");
        console.log(res);
      });
    },
    // 封装预热销毁函数
    async closePreheat() {
      await Axios({
        method: "post",
        url: this.$cloudBaseurl + "solarEnergyCamera/closePower",
        data: { openSourceMark: this.openSourceMark }
      }).then(
        res => {
          console.log(res);
          console.log("预热销毁成功");
        },
        err => {
          console.log(err);
        }
      );
    },
    async getcams(id) {
      let zhurl =
        this.$cloudBaseurl +
        `cameraOperation/judgeFlow?cameraId=${id}`;
      const { data: res } = await Axios({
        method: "get",
        url: zhurl
      });
      // console.log(res)
      let data = res.data;
      if (res.code === 1) {
        // console.log(data.basis)
        if (data.flowUse) {
          this.$message.success(
            "总流量" + data.flowTotal + "已使用流量" + data.flowUse
          );
        }
        return data.basis;
      } else {
        this.$message.warning(res.msg || "未知错误！！！");
        return false;
      }
    },
    // 拼接摄像头地址
    setcameraurl() {
      // 判断有无摄像头
      if (this.cameraInfoList) {
        if (this.cameraInfoList.cameraInfo.status === 1) {
          // alert(this.cameraInfoList['deviceSerial'])
          this.cameraurl = "";
          // console.log(this.cameraInfoList)
          if (this.cameraInfoList) {
            this.cameraurl = `https://open.ys7.com/ezopen/h5/iframe?url=ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live&autoplay=1&accessToken=${this.cameraInfoList["accessToken"]}`;
          }
          // this.$emit("nowcameraurl", this.cameraurl);
          this.$emit("nowcameraurl", this.cameraurl, { id: 'video-container', accessToken: this.cameraInfoList["accessToken"], url: `ezopen://open.ys7.com/${this.cameraInfoList["deviceSerial"]}/${this.cameraInfoList["channelNo"]}.hd.live`, autoplay: 1, footer: ['fullScreen'] });

          this.timer = setTimeout(() => {
            this.$emit("nowcameraurl", "timeout");
          }, 1000 * 60 * 20);
        } else {
          this.$message.warning("摄像头不在线！请稍后重试");
          this.$emit("nowcameraurl", "notCameraData");
          return;
        }
      } else {
        // 无 传空
        // this.cameraurl = null
        this.$emit("nowcameraurl", "notCameraList");
      }
    },
    // 开始控制
    async startcontrol(i) {
      this.timer = setTimeout(() => {
        this.$emit("nowcameraurl", "timeout");
      }, 1000 * 60 * 20);
      let params = {
        cameraId: this.cameraId,
        direction: i,
        speed: 1
      };
      await nowpagereq.controlcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 1000);
    },
    // 镜像翻转
    async mirrorcontrol() {
      this.timer = setTimeout(() => {
        this.$emit("nowcameraurl", "timeout");
      }, 1000 * 60 * 20);
      let params = {
        cameraId: this.cameraId,
        direction: 2,
        speed: 1
      };
      await nowpagereq.mirrorcamera(params);
      setTimeout(() => {
        this.stopcontrol();
      }, 500);
    },
    // 停止控制
    stopcontrol() {
      let params = {
        cameraId: this.cameraId
      };
      nowpagereq.stopcamera(params).then(res => {
        // console.log(res)
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.allcameramain {
  height: 100%;
  width: 100%;
}
.cameralistmain {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: nowrap column;
  align-items: center;
  position: relative;
  justify-content: space-between;
  .camera-title {
    // width: 80%;
    width: 180px;
    height: 38px;
    line-height: 36px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 100% 100%;
    background-image: url("~@/assets/commont/top-hexagon.png");
    // border: 1px solid #ccc;
    position: absolute;
    font-size: 18px;
    z-index: 6;
    margin: 0 auto;
    text-align: center;
    // background: #005d7b;
  }
  .camera-zlist {
    width: 100%;
    // padding-top:20px;
    height: 100%;
    // position: relative;
    z-index: 5;
    margin-top: 18px;
    // top: -15px;
    // display: flex;
    // flex-flow: nowrap column;
    // align-items: center;
    // justify-content: center;
    background-color: #044a6090;
    border: 1px solid #1093b6;
    border-radius: 8px 8px 0px 0px;
    overflow-y: auto;
  }
  .camera-item {
    cursor: pointer;
    width: 80%;
    font-weight: 700;
    font-size: 16px;
    line-height: 40px;
    margin: 0 auto;
    height: 40px !important;
    color: #fff;
    margin-bottom: 10px;
    border-radius: 5px;
    text-align: center;
    // border: 1px solid #fff;
    overflow: hidden;
    border: 1px solid #005e77;
    text-overflow: ellipsis;
    border-radius: 29px;
    white-space: nowrap;
  }
  .camera-item:nth-child(1) {
    margin-top: 25px;
  }
  .camera-item:hover {
    background-image: linear-gradient(180deg, #0098b7 0%, #76f5d2 100%);
  }
  .beselect {
    background-image: linear-gradient(180deg, #0098b7 0%, #76f5d2 100%);
  }
}
.cameracontrol {
  width: 100%;
  height: calc(70% - 10px);
  margin-top: 10px;
  .controne {
    width: 100%;
    height: 65%;
    background-image: url("~@/assets/camera/ctrlBg.png");
    background-repeat: no-repeat;
    background-size: 90%;
    background-position: center;
    position: relative;
    .contro-m {
      // border: #46c357 1px solid;
      position: relative;
      width: 25%;
      height: 25%;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 40px;
      cursor: pointer;
    }
    .contro-m:hover {
      font-size: 48px;
    }
    .z-top {
      top: 10%;
      left: 37%;
    }
    .z-left {
      top: 12%;
      left: 11%;
    }
    .z-right {
      top: -13%;
      left: 63%;
    }
    .z-bottom {
      top: -10%;
      left: 37%;
    }
    .z-mirror {
      top: -63%;
      left: 37%;
    }
  }
  .controtwo {
    width: 100%;
    height: 15%;
    display: flex;
    justify-content: space-around;
    .contro-left {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformLeft.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-right: 30px;
      cursor: pointer;
      font-size: 18px;
    }
    .contro-left:hover {
      font-size: 20px;
    }
    .contro-right {
      width: 48%;
      height: 100%;
      display: flex;
      justify-content: flex-start;
      align-items: center;
      // border: #46c357 1px solid;
      background-image: url("~@/assets/camera/transformRight.png");
      background-repeat: no-repeat;
      background-size: 70%;
      background-position: center;
      padding-left: 30px;
      cursor: pointer;
      font-size: 18px;
    }
    .contro-right:hover {
      font-size: 20px;
    }
  }
  .controthree {
    width: 100%;
    height: 17%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .greenbut {
      width: 28%;
      height: 60%;
      background-color: rgb(132, 180, 111);
      border-radius: 5px;
      text-align: center;
      display: flex;
      justify-content: center;
      align-items: center;
      font-weight: 600;
      font-size: 22px;
      cursor: pointer;
    }
    .greenbut:hover {
      font-size: 24px;
    }
  }
}
</style>
